package com.zmx.job.admin.core;

import com.zmx.core.server.MonitorClient;
import com.zmx.core.server.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: 初始化服务端监控
 * @ClassName: InitServer
 * @Author zhaomxr
 * @Date 2021-12-15 13:53
 */
@Configuration
public class InitServer {

    @Value("${auth.username}")
    private String username;

    @Value("${auth.password}")
    private String password;

    @Value("${auth.secret}")
    private String secret;

    @Value("${job.monitor.interval.time:90}")
    private int monitorIntervalTime;

    /**
     * 启动当前服务监听8765端口，当前实例充当服务注册服务端（仅用于获取所有客户端实例，监测各客户端实例是否存活）
     * @return
     */
    @Bean("server")
    public Server server() {
        Server server = new Server(8765, username, password, secret);
        server.start();
        return server;
    }

    /**
     * 监测节点是否在线 判断服务是否在指定时间内发送心跳信息
     * @return
     */
    @Bean("monitorClient")
    public MonitorClient monitorClient() {
        MonitorClient monitorClient = new MonitorClient(monitorIntervalTime);
        monitorClient.start();
        return monitorClient;
    }
}
